<template>
	<view class="page">
		<view class="topbg">
			<image src="../../static/image/other/userimg/Login-Screen-6_01.png" mode="" class="image"></image>
		</view>
		<view class="content">
			<view class="title">注册账号</view>
			<view class="t-text">Welcome to register 好多个你</view>
			<view class="inpcon">
				<view class="list-top">账号</view>
				<view class="list">
					<view style="height: 100%;display: flex;align-items: center;">
						<image src="../../static/image/other/userimg/Vector(1)_1.png" mode="" style="width: 26rpx;height: 34rpx;"></image>
					</view>
					<view class="li_ri">
						<input type="text" v-model="phoneNumber" placeholder="请输入手机号" placeholder-class="placeholder"/>
					</view>
				</view>
			</view>
			
			<view class="inpcon">
				<view class="list-top">验证码</view>
				<view class="list">
					<view style="height: 100%;display: flex;align-items: center;">
						<image src="../../static/image/other/userimg/Vector_1.png" mode="" class="li_le"></image>
					</view>
					<view class="li_ri">
						<input type="text" v-model="verCode" placeholder="请输入验证码" placeholder-class="placeholder"/>
					</view>
					<view @click="sendCode" style="height: 100%;display: flex;align-items: center;font-size: 24rpx;text-align: end;" >
						<text style="width: 160rpx;color: #00E486;">{{isSending? '重新获取('+ intervalTime + 's)':'获取验证码'}}</text>
					</view>
				</view>
			</view>
			
			
			<view class="inpcon">
				<view class="list-top">新密码</view>
				<view class="list">
					<view style="height: 100%;display: flex;align-items: center;">
						<image src="../../static/image/other/userimg/Vector(1).png" mode="" class="li_le"></image>
					</view>
					<view class="li_ris">
						<input type="text" v-model="password" :password="ispass" placeholder="请输入新密码" placeholder-class="placeholder"/>
					</view>
					<view style="height: 100%;display: flex;align-items: center;" @click="ispass = !ispass">
						<image v-if="ispass" src="../../static/image/other/userimg/Vector(2).png" mode="" class="li_pass"></image>
						<image v-else src="../../static/image/other/userimg/Group.png" mode="" class="li_pass"></image>
					</view>
				</view>
			</view>
			
			<view class="inpcon">
				<view class="list-top">确认新密码</view>
				<view class="list">
					<view style="height: 100%;display: flex;align-items: center;">
						<image src="../../static/image/other/userimg/Vector(1).png" mode="" class="li_le"></image>
					</view>
					<view class="li_ris">
						<input type="text" v-model="confirmpassword" :password="isnewpass" placeholder="请再次输入新密码" placeholder-class="placeholder"/>
					</view>
					<view style="height: 100%;display: flex;align-items: center;" @click="isnewpass = !isnewpass">
						<image v-if="isnewpass" src="../../static/image/other/userimg/Vector(2).png" mode="" class="li_pass"></image>
						<image v-else src="../../static/image/other/userimg/Group.png" mode="" class="li_pass"></image>
					</view>
				</view>
			</view>
			
			<view class="btn" @click="verRegister" v-if="!isLoadding">立即注册</view>
			<view class="btn" v-if="isLoadding">立即注册</view>

		</view>
			
		</view>
	</view>
</template>

<script>
	import {
		sendmobilecode,
		registerApp
	} from "@/api/user.js"
	export default {
		data() {
			return {
				background: {
					backgroundColor: 'rgba(0,0,0,0)',
				},
				verCode: '', //验证码
				isLoadding: false,
				phoneNumber: '', //手机号
				password: '', //密码
				confirmpassword: '', //确认密码
				timer: null, // 用于倒计时
				intervalTime: 0, // 倒计时时间（秒）
				isSending: false ,// 防止重复点击
				ispass:true,
				isnewpass:true
			}
		},
		onLoad() {
			
		},
		methods: {
			// 发送验证码
			sendCode() {
				if (!this.phoneNumber) {
					uni.showToast({
						title: '请输入手机号',
						icon: 'none'
					})
					return;
				}
				if (!/^1[3-9]\d{9}$/.test(this.phoneNumber)) {
					uni.showToast({
						title: '手机号格式不正确',
						icon: 'none'
					})
					return;
				}
				if (this.isSending) {
					uni.showToast({
						title: '验证码发送中，请稍候...',
						icon: 'none'
					})
					return;
				}

				// 禁用按钮并启动倒计时
				this.isSending = true;
				this.startCountdown();

				// 调用后端接口发送验证码
				sendmobilecode({
					mobile: this.phoneNumber,
					type: 1
				}).then(res => {
					if (res.errno == 0) {
						uni.showToast({
							title: '验证码发送成功'
						})

					} else {
						uni.showToast({
							title: '验证码发送失败：' + res.msg,
							icon: 'none'
						})
						// 如果发送失败，需要重置倒计时和按钮状态
						this.resetCountdown();
					}
				}).catch(err => {
					uni.showToast({
						title: '请求失败，请检查网络',
						icon: 'none'
					})

					this.resetCountdown();
				})

			},
			// 开始倒计时
			startCountdown() {
				this.intervalTime = 60;
				this.timer = setInterval(() => {
					this.intervalTime--;
					if (this.intervalTime <= 0) {
						this.resetCountdown();
					}
				}, 1000);
			},
			// 重新倒计时
			resetCountdown() {
				clearInterval(this.timer);
				this.intervalTime = 60;
				this.isSending = false;
			},
			// 注册
			register() {
				if (this.isLoadding) {
					uni.showToast({
						title: '请勿重复点击',
						icon: 'none'
					})
					return
				}
				uni.showLoading({
					title: '加载中'
				})
				this.isLoadding = true;
				registerApp({
					code: this.verCode,
					mobile: this.phoneNumber,
					password: this.password,
					repassword: this.confirmpassword
				}).then(res => {

					if (res.errno == 0) {
						uni.showToast({
							title: '注册成功',
							icon: 'none'
						})
						setTimeout(() => {
							this.isLoadding = false
							uni.redirectTo({
								url: '/pages/login/index'
							})
						}, 1000)

					} else {
						this.isLoadding = false
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}).catch(res => {
					this.isLoadding = false
					uni.showToast({
						title: '注册失败',
						icon: 'none'
					})
				})
			},
			// 注册校验
			verRegister() {
				if (!this.phoneNumber) {
					uni.showToast({
						title: '请输入手机号',
						icon: 'none'
					})
					return;
				}
				if (!/^1[3-9]\d{9}$/.test(this.phoneNumber)) {
					uni.showToast({
						title: '手机号格式不正确',
						icon: 'none'
					})
					return;
				}
				if (!this.password) {
					uni.showToast({
						title: '请输入密码',
						icon: 'none'
					})
					return;
				}
				if (!this.confirmpassword) {
					uni.showToast({
						title: '请输入确认密码',
						icon: 'none'
					})
					return;
				}
				if(!this.verCode){
					uni.showToast({
						title: '请输入验证码',
						icon: 'none'
					})
					return;
				}
				if (this.password != this.confirmpassword) {
					uni.showToast({
						title: '两次密码不一致，请重新输入',
						icon: 'none'
					})
					return;
				}
				this.register();

			},
			// 获取提现记录
			getWithdrawalLog() {
				withdrawalLog({
					uid: uni.getStorageSync("userId")
				}).then(res => {
					if (res.errno == 0) {
						this.list = res.data;
					}
				})
			},
			// 获取用户信息
			getUserinfo() {
				getUserinfo({
					uid: uni.getStorageSync("userId")
				}).then(res => {
					console.log(res.data, 'getUserinfo')
					this.balance = res.data.balance;
				}).catch(err => {

				})
			},
		}
	}
</script>

<style>
.page{
	height: 100vh;
	width: 100%;
	overflow: hidden;
}
.image{
	width: 100%;
	height: 100%;
}
.placeholder{
	color: #A4A4A4;
}
.topbg{
	width: 100%;
	height: 418rpx;
}
.content{
	background:url("../../static/image/other/userimg/Login-Screen-6_02.png") no-repeat;
	background-size: 100% 100%;
	width: 100%;
	height: calc(100% - 250rpx);
	border-radius: 74rpx 74rpx 0 0;
	margin-top: -152rpx;
	position: relative;
	z-index: 6;
	padding:60rpx;
	box-sizing: border-box;
	overflow-y: auto;
}
.title{
	font-size: 68rpx;
	color: #EFEFEF;
}
.t-text{
	font-size: 28rpx;
	color: #A4A4A4;
}
.inpcon{
	margin-top: 40rpx;
}
.list-top{
	color: #A4A4A4;
	font-size: 28rpx;
	margin-bottom: 20rpx;
}
.list{
	display: flex;
	align-items: center;
	justify-content: space-between;
	border: 1rpx solid rgba(255, 255, 255, 0.5);
	background: linear-gradient(to right,#3d5252,#192d2d);
	/* background:radial-gradient(151.92% 127.02% at 15.32% 21.04%, #ffffff33 0%, #ffffff0a 77.08%, #ffffff00 100%); */
	height: 90rpx;
	border-radius: 16rpx;
	padding: 0 40rpx;
	box-sizing: border-box;
	color: #A4A4A4;
	font-size: 28rpx;
}
.li_le{
	width: 32rpx;
	height: 32rpx;
	margin-right: 20rpx;
	display: flex;
	align-items: center;
	justify-content: center;
}
.li_ri{
	width: calc(100% - 60rpx);
}
.li_pass{
	width: 30rpx;
	height: 24rpx;
}
.li_ris{
	width: calc(100% - 90rpx);
}
.btn{
	height: 100rpx;
	line-height: 100rpx;
	text-align: center;
	margin-top: 60rpx;
	color: #fff;
	border-radius: 24rpx;
	background: linear-gradient(to right,#00CCBA,#00E486);
}

.bot{
	font-size: 24rpx;
	color: #A4A4A4;
	text-align: center;
	margin-top: 50rpx;
}
</style>